//
//  WKSchoolViewController.m
//  Walk
//
//  Created by cesh on 16/9/21.
//  Copyright © 2016年 cesh. All rights reserved.
//

#import "WKSchoolViewController.h"

@interface WKSchoolViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *leftTableView;
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;
@property (weak, nonatomic) IBOutlet UIImageView *backImg;

@property (strong, nonatomic) NSArray *data;
@end

@implementation WKSchoolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.leftTableView.dataSource = self;
    self.leftTableView.delegate = self;
    
    self.rightTableView.dataSource = self;
    self.rightTableView.delegate = self;
    
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.leftTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"left"];
    [self.rightTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"right"];
    
    self.backImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapOnBack = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)];
    [self.backImg addGestureRecognizer:tapOnBack];
    
    [self getData];
}

- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //接收类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         @"text/plain",
                                                         nil];
    [manager GET:[NSString stringWithFormat:@"%@Info/schoolList", URL] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"学校列表%@", responseObject);
        self.data = responseObject[@"data"];
        [self.leftTableView reloadData];
        [self.rightTableView reloadData];
        
        [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}
#pragma mark - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.data == nil) {
        if ([tableView isEqual:self.leftTableView]) {
            return 1;
        }else {
            return 3;
        }
    }
    
    else{
        if([tableView isEqual:self.leftTableView]) {
            return self.data.count;
        }else {
            NSArray *array = self.data[self.leftTableView.indexPathForSelectedRow.row][@"son"];
            return array.count ;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:self.leftTableView]) {
        UITableViewCell *cell = [self.leftTableView dequeueReusableCellWithIdentifier:@"left"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]init];
        }
        cell.textLabel.text = self.data[indexPath.row][@"name"];
        cell.textLabel.font = [UIFont systemFontOfSize:14.0];
        cell.textLabel.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
        
        return cell;
    }
    else {
        UITableViewCell *cell = [self.rightTableView dequeueReusableCellWithIdentifier:@"right"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]init];
        }
        NSArray *array = self.data[self.leftTableView.indexPathForSelectedRow.row][@"son"];
        
        cell.textLabel.text = array[indexPath.row][@"name"];
        cell.textLabel.font = [UIFont systemFontOfSize:14.0];
        cell.textLabel.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
        

        
        return cell;
    }
}

#pragma mark - table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:self.leftTableView]) {
        [self.rightTableView reloadData];
    }
    else {
        [self dismissViewControllerAnimated:YES completion:nil];
        if ([self.delegate respondsToSelector:@selector(reloadSchool:schoolID:)]) {
            NSInteger leftTableViewIndex = self.leftTableView.indexPathForSelectedRow.row;
            NSString *school = self.data[leftTableViewIndex][@"son"][indexPath.row][@"name"];
            NSString *schoolID = self.data[leftTableViewIndex][@"son"][indexPath.row][@"id"];
            [self.delegate reloadSchool:school schoolID:schoolID];
        }
    }
}

@end
